package com.light.ui;

import com.light.constant.Constant;
import com.light.model.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.concurrent.TimeUnit;

/**
 * @Author MoonlightL
 * @ClassName: MyPanel
 * @ProjectName flappy_bird
 * @Description: 面板
 * @DateTime 2023/3/13, 0013 10:57
 */
public class MyPanel extends JPanel {

    private Background background;

    private Land land;

    private Score score;

    private Title title;

    private Bird bird;

    private Tutorial tutorial;

    private PlayButton playButton;

    private Pipe pipe1;

    private Pipe pipe2;

    private GameOver gameOver;

    private ScorePanel scorePanel;

    // 状态 0：初始化 1：进行中 2：已结束
    private volatile int status;

    private int totalScore = 0;

    public MyPanel() {
        this.setSize(Constant.WIN_WIDTH, Constant.WIN_HEIGHT);
        this.setLayout(null);
        this.background = new Background(0, 0);
        this.land = new Land(0, Constant.LAND_Y);
        this.score = new Score(132, 40, this.totalScore);
        this.title = new Title(55, 100);
        this.bird = new Bird(60, 180);
        this.tutorial = new Tutorial(87, 170);
        this.playButton = new PlayButton(86, 300);
        this.pipe1 = new Pipe(288, 0);
        this.pipe2 = new Pipe(470, 0);
        this.gameOver = new GameOver(42, 100);
        this.scorePanel = new ScorePanel(25, 160);
        this.add(playButton);
        new Thread(() -> {
            this.action();
        }).start();
    }

    public void action() {
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (status == 1 && e.getKeyCode() == KeyEvent.VK_UP) {
                    bird.tap();
                }
            }
        });

        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (status == 1) {
                    bird.tap();
                }
            }
        });

        this.playButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (status == 2) {
                    bird.reset();
                    pipe1.reset();
                    pipe2.reset();
                    score.reset();
                    totalScore = 0;
                }
                status = 1;
            }
        });

        while (true) {
            try {
                this.land.keepMove(this.land.getX() - Constant.LAND_MOVE_STEP, this.land.getY());
                if (this.status == 0) {
                    this.bird.fly();
                } else if (this.status == 1) {
                    this.bird.fly();
                    this.bird.down();
                    this.pipe1.keepMove(this.pipe1.getX() - Constant.PIPE_MOVE_STEP, this.pipe1.getY());
                    this.pipe2.keepMove(this.pipe2.getX() - Constant.PIPE_MOVE_STEP, this.pipe2.getY());
                    if (this.bird.checkHit() ||
                        this.pipe1.checkHit(this.bird) ||
                        this.pipe2.checkHit(this.bird)) {
                        this.status = 2;
                    }
                    if (this.pipe1.cross(this.bird) || this.pipe2.cross(this.bird)) {
                        this.score.modify(++totalScore);
                    }
                } else if (this.status == 2) {
                    this.bird.stop();
                    this.pipe1.keepMove(this.pipe1.getX(), this.pipe1.getY());
                    this.pipe2.keepMove(this.pipe2.getX(), this.pipe2.getY());
                }

                TimeUnit.MILLISECONDS.sleep(150);

                this.repaint();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        this.background.draw(g2);
        this.land.draw(g2);
        this.bird.draw(g2);
        if (this.status == 0) {
            this.score.draw(g2);
            this.title.draw(g2);
            this.tutorial.draw(g2);
            this.playButton.draw(g2);
        } else if (this.status == 1) {
            this.pipe1.draw(g2);
            this.pipe2.draw(g2);
            this.score.draw(g2);
        } else if (this.status == 2) {
            this.pipe1.draw(g2);
            this.pipe2.draw(g2);
            this.gameOver.draw(g2);
            this.scorePanel.draw(g2);
            this.playButton.draw(g2);
            g2.setColor(Color.WHITE);
            g2.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
            g2.drawString(this.totalScore + "", 210, 210);
        }
        this.requestFocus();
    }
}
